<?php

namespace App\Console\Commands\Chanjet;

use App\Http\Controllers\Chanjet\BaseController;
use App\Http\Controllers\Chanjet\TplusAccount;
use App\Http\Controllers\Chanjet\TplusCommonController;
use App\Models\Chanjet\TplusToken;
use App\Services\Chanjet\ChanjetRequest;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class Account extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:Account';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取Tplus数据-账套列表';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //定时调用获取账套
        $tokenList = TplusToken::select(['access_token','org_id'])->get();
        foreach ($tokenList as $item){
            $res = ChanjetRequest::getInstance()->accountAPIGetAccList([""=>""],$item['org_id'],$item['access_token']);
            foreach ($res as $k=>$v){
                $orgIdExist = \App\Models\Chanjet\TplusAccount::where("org_id",$v["orgId"])->first();
                if($orgIdExist){
                    continue;
                }
                $data = [
                    "accNum" => $v["accNum"],
                    "accName" => $v["accName"],
                    "accOrgName" => $v["accOrgName"],
                    "org_id" => $v["orgId"],
                    "res" => json_encode($v)
                ];
                \App\Models\Chanjet\TplusAccount::create($data);
            }
        }
        Log::info($this->description."执行成功");
        return 0;
    }
}
